﻿using B.S.Data.MES.API.Applications.WorkOrderManagement.Commands;
using B.S.Data.MES.API.Dto.WorkDtos;
using B.S.Domain.BasicData.Factory_modeling.Production_line_management;
using B.S.Domain.BasicData.MaterialManagement.Product_material;
using B.S.Domain.Process.ProcessrouteName;
using B.S.Domain.Ticket;
using B.S.Domain.基础数据.客户管理;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using Microsoft.EntityFrameworkCore;
using NPOI.SS.Formula.Functions;
using SqlSugar;

namespace B.S.Data.MES.API.Applications.WorkOrderManagement.CommandHandlers
{
    public class GetWorkOrderCommandHandler : IRequestHandler<GetWorkOrderCommand, APIRequst<APIPageing<List<WorkOrderDTO>>>>
    {
        private readonly IBaseRepository<WorkOrder> _workorderRepository;
        private readonly IBaseRepository<client> _clientRepository;
        private readonly IBaseRepository<Product_material> _product_materialRepository;
        private readonly IBaseRepository<Processroute> _processrouteRepository;
        private readonly IBaseRepository<Production_line_management> _production_line_managementRepository;
        private readonly IBaseRepository<DingDan> _dingdanRepository;
        public GetWorkOrderCommandHandler(IBaseRepository<WorkOrder> workorderRepository, IBaseRepository<client> clientRepository, IBaseRepository<Product_material> product_materialRepository
            , IBaseRepository<Processroute> processrouteRepository, IBaseRepository<Production_line_management> production_line_managementRepository,
            IBaseRepository<DingDan> dingdanRepository)
        {
            _workorderRepository = workorderRepository;
            _clientRepository = clientRepository;
           _product_materialRepository = product_materialRepository;
            _processrouteRepository = processrouteRepository;
            _production_line_managementRepository = production_line_managementRepository;
            _dingdanRepository=dingdanRepository;
        }
        public   Task<APIRequst<APIPageing<List<WorkOrderDTO>>>> Handle(GetWorkOrderCommand request, CancellationToken cancellationToken)
        {
            APIRequst<APIPageing<List<WorkOrderDTO>>> result = new APIRequst<APIPageing<List<WorkOrderDTO>>>();
            result.Code = RequstCode.成功;
                result.Msg = RequstCode.成功.ToString();
            var list = from a in _workorderRepository.GetAll()
                       join b in _clientRepository.GetAll() on a.CustomerId equals b.CustomerID
                       join c in _product_materialRepository.GetAll() on a.ProductId equals c.Productid
                       join d in _processrouteRepository.GetAll() on a.ProcessroutId equals d.Id
                       join e in _production_line_managementRepository.GetAll() on a.ProductionlineId equals e.ProductionlinePK
                       join f in _dingdanRepository.GetAll() on a.DingDanId equals f.Id
                       where !a.isDel
                       select new WorkOrderDTO
                       {
                           WorkId = a.WorkId,
                           WorkCode=a.WorkCode,
                           Type=a.Type,
                           CustomerId=b.CustomerID,
                           Customername=b.Customername,
                           ProductId=c.Productid,
                           ProductName=c.ProductName,
                           ProcessroutId=d.Id,
                           ProcessrouteName=d.ProcessrouteName,
                           ProductionlineId=e.ProductionlinePK,
                           Nameofproductionline=e.Nameofproductionline,
                           EndTime=a.EndTime,
                           StartTime=a.StartTime,
                           WorkNum=a.WorkNum,
                           IsSkipprocess=a.IsSkipprocess,
                           Scoure=a.Scoure,
                           States=a.States,
                           Remark=a.Remark,
                           DingDanId=f.Id,
                           DingDanBianHao=f.DingDanBianHao,
                           DingDanRiQi=f.DingDanRiQi,
                           ZongJinE=f.ZongJinE,
                           ZhuangTai=f.ZhuangTai,

                       };
            //编码（模糊查询）
            if(!string.IsNullOrEmpty(request.WorkCode))
            {
                list=list.Where(x=>x.WorkCode.Contains(request.WorkCode));
            }
            if(request.Type>0)
            {
                list=list.Where(x=>x.Type==request.Type);
            }
            if(!string.IsNullOrEmpty(request.Customername))
            {
                list=list.Where(x=>x.Customername.Contains(request.Customername));
            }
            if(request.States>0)
            {
                list=list.Where(x=>x.States==request.States);
            }
            if(request.Scoure>0)
            {
                list=list.Where(x=>x.Scoure==request.Scoure);
            }
        
            int recordcount = list.Count();
            int pagecount = (int)Math.Ceiling(recordcount * 1.0 / request.PageSize);

            list = list.OrderByDescending(x => x.WorkId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);

            APIPageing<List<WorkOrderDTO>> paing = new APIPageing<List<WorkOrderDTO>>();
            paing.TotalCount = recordcount;
            paing.Total = pagecount;
            paing.Data =  list.ToList();
            result.Data = paing;
            return   Task.FromResult(result);


        }
    }
}
